In [1]:
from collections import OrderedDict
import sys
import base_node_rpc.node as bn
sys.path.insert(0, '../..')
import magnet_control as mc
from nadamq.NadaMq import cPacket, cPacketParser, PACKET_TYPES, PACKET_NAME_BY_TYPE
from nanopb_helpers import dump_message
from serial import Serial
import pandas as pd
import numpy as np

Connect to device


In [14]:
import time
# serial_device = Serial('/dev/ttyUSB0', baudrate=115200)
serial_device = Serial('/dev/ttyUSB2', baudrate=115200)
# serial_device = Serial('/dev/ttyACM0', baudrate=115200)
proxy = mc.Proxy(serial_device)

retry_count = 5
for i in range(retry_count):
    try:
        properties = proxy.properties()
    except IOError:
        if i >= retry_count - 1:
            raise
print properties


base_node_software_version                                                 0.11
name                                                              magnet_control
manufacturer                                                        Wheeler Lab
url                           http://github.com/wheeler-microfluidics/servo-...
software_version                                                            0.1
dtype: object

In [17]:
print '%(serial_number)03d' % {'serial_number': 3}


003

In [16]:
properties.to_dict()


Out[16]:
{'base_node_software_version': '0.11',
 'manufacturer': 'Wheeler Lab',
 'name': 'magnet_control',
 'software_version': '0.1',
 'url': 'http://github.com/wheeler-microfluidics/magnet-control.git'}

In [15]:
properties['name']


Out[15]:
'magnet_control'

In [9]:
import datetime as dt

In [11]:
dt.timedeltakkkkkkkkkkkkkkkkkk


Out[11]:
datetime.timedelta

Dump initial configuration and motor state


In [110]:
config = mc.Config.FromString(proxy.serialize_config().tostring())
print dump_message(config)


    baud_rate: 115200
serial_number: 0
  i2c_address: 0

In [15]:
proxy.servo_attach(9)

In [16]:
proxy.servo_write(110)

In [17]:
proxy.servo_write(75)

In [18]:
from IPython.html.widgets import interactive

In [24]:
def set_state(state=False):
    if state:
        proxy.servo_write(75)
    else:
        proxy.servo_write(103)
        

interactive(set_state)

In [12]:
serial_device.baudrate


Out[12]:
115200

In [ ]:
serial_device.close()

In [22]:



Out[22]:
base_node_software_version                                                 0.11
name                                                              magnet_control
manufacturer                                                        Wheeler Lab
url                           http://github.com/wheeler-microfluidics/servo-...
software_version                                                            0.1
dtype: object

Basic interfacing


In [5]:
# Set several motor attributes at once using `update_state` method.
proxy.update_state(motor_delay_us=int(1e3), motor_enabled=True, motor_pulse_us=20)


Out[5]:
1

In [6]:
# Reset current motor position as position 0.
proxy.motor_set_home()

In [7]:
# Query current motor position.
proxy.position()


Out[7]:
0

In [8]:
# Set motor speed (steps per second).
proxy.motor_set_speed(1000)

In [9]:
# Set target position to half turn (assuming 200 steps/revolution motor).
proxy.set_target_position(100)

In [10]:
# Set target position origin.
proxy.set_target_position(0)

In [11]:
# Move steps relative to current position.
# Positive values are clockwise, negative counter-clockwise.
proxy.move(-1)

In [12]:
# If the motor is enabled, start moving the motor at 20 steps per second.
proxy.motor_start(-400)

In [13]:
# Stop the motor (but do not disable it).
proxy.motor_stop()

Interactive control


In [14]:
from IPython.html.widgets import interactive


def update_motor(position=0, speed=1000, enabled=False, microstep_mode='full'):
    if enabled:
        # Change microstep setting (STEP_SETTINGS.(full,half,quarter,eighth,sixteeth)
        proxy.set_MS(*getattr(STEP_SETTINGS, microstep_mode).values)
        # Set motor speed (steps per second).
        proxy.motor_set_speed(speed)
        proxy.update_state(motor_enabled=True)
        proxy.set_target_position(position)
    else:
        proxy.update_state(motor_enabled=False)

    
interactive(update_motor, position=(-400, 400), speed=(200, 1600),
            microstep_mode=('full', 'half', 'quarter', 'eighth', 'sixteenth'))